package com.yao.controller;

import com.yao.VO.ResultVO;
import com.yao.converter.OrderForm2OrderDTOConverter;
import com.yao.dto.OrderDTO;
import com.yao.enums.ResultEnum;
import com.yao.exception.WeixinSellException;
import com.yao.form.OrderForm;
import com.yao.service.BuyerService;
import com.yao.service.OrderService;
import com.yao.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: 姚宏武
 * Date: 2017/11/20
 * Time: 13:42
 * Email：401369870@qq.com
 * Description: 订单控制器
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;

    /**
     * 订单创建
     * @param orderForm 前台创建订单时传递的数据
     * @param bindingResult 对传递的数据做校验后的结果
     * @return
     */
    @PostMapping(value = "/create")
    public ResultVO<Map<String,String>> create(@Valid OrderForm orderForm, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            log.error("【创建订单】参数不正确,orderForm={}",orderForm);
            throw new WeixinSellException(ResultEnum.PARAM_ERROR.getCode(),
                bindingResult.getFieldError().getDefaultMessage());
    }

    OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
        log.error("【创建订单】购物车不能为空");
        throw new WeixinSellException(ResultEnum.CART_EMPTY);
    }

        OrderDTO createResult = orderService.create(orderDTO);
        Map<String, String> map = new HashMap<>();
        map.put("orderId", createResult.getOrderId());

        return ResultVOUtil.success(map);
    }

    /**
     * 查询订单列表
     * @param openid 用户微信openid
     * @param page 查询的页码
     * @param size 每页的数据条数
     * @return
     */
    @GetMapping(value = "/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page",defaultValue = "0") Integer page,
                                         @RequestParam(value = "size",defaultValue = "10") Integer size){

        if (StringUtils.isEmpty(openid)) {
            log.error("【查询订单列表】用户openid为空");
            throw new WeixinSellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest request = new PageRequest(page, size);
        Page<OrderDTO> orderDTOPage = orderService.findList(openid, request);
        return ResultVOUtil.success(orderDTOPage.getContent());
    }

    /**
     * 查询订单详情
     * @param openid 用户微信的openid
     * @param orderId 订单id
     * @return
     */
    @GetMapping(value = "/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId){
        OrderDTO orderDTO = buyerService.findOrderOne(openid, orderId);
        return ResultVOUtil.success(orderDTO);
    }

    /**
     * 取消订单
     * @param openid 用户微信的openid
     * @param orderId 订单id
     * @return
     */
    @PostMapping(value = "/cancel")
    public ResultVO cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId){
       buyerService.cancelOrder(openid, orderId);
        return ResultVOUtil.success();
    }

}
